/**
 * Copyright(c)2012 Beijing PeaceMap Co.,Ltd.
 * All right reserved. 
 */
package com.pmc.dwa.common.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.StringUtils;

import com.pmc.dwa.common.exception.PmcRuntimeException;

/**
 * @description 获取属性文件
 * @author aokunsang
 * @date 2013-1-6
 */
public class ConfigParameter {

	final static String DEFAULT_CLASSPATH = "classpath:config/";
	
	/**
	 * 获取属性文件值
	 * @param classpath  属性文件路径，例如：classpath:config/
	 * @param fileName   属性文件名字
	 * @param key        键值
	 * @return
	 */
	public static String getParamVal(String classpath,String fileName,String key){
		ResourceLoader loader = new DefaultResourceLoader();
		Properties prop = null;
		try {
			classpath = StringUtils.replace(classpath,"\\","/");
			classpath = classpath.endsWith("/") ? classpath : classpath+"/";
			InputStream is = loader.getResource(classpath+fileName).getInputStream();
			prop = new Properties();
			prop.load(is);
		} catch (IOException e) {
			throw new PmcRuntimeException("-------获取属性文件失败-------", e);
		}
		return prop.getProperty(key);
	}
	
	/**
	 * 获取属性文件值[默认路径为：classpath:config/]
	 * @param fileName 属性文件名字
	 * @param key      键值
	 * @return
	 */
	public static String getParamVal(String fileName,String key){
		return getParamVal(DEFAULT_CLASSPATH, fileName, key);
	}
}
